import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

///// State<T>的混入内容
mixin TStateMixin<T extends StatefulWidget> on State<T> {
  /// 混入的GlobalKey<ScaffoldState> sk
  GlobalKey<ScaffoldState> tsk = GlobalKey<ScaffoldState>();
}

///// State<T>的扩展方法
extension StateExtends<T extends TStateMixin> on State {
  MediaQueryData get mediaQuery => MediaQuery.of(context);

  Size get screenSize => this.mediaQuery.size;

  ThemeData get themeData => Theme.of(context);

  Color get primaryColor => this.themeData.primaryColor;

  /// 当前的GlobalKey<ScaffoldState>
  GlobalKey<ScaffoldState> _getSK() {
    return (this as TStateMixin).tsk;
  }

  /// 提示基础版
  ScaffoldFeatureController<SnackBar, SnackBarClosedReason> tShowMessage({String message = '', bool removeOldMessage = true}) {
    if (removeOldMessage) {
      _getSK().currentState.hideCurrentSnackBar(reason: SnackBarClosedReason.hide);
    }
    SnackBar sb = SnackBar(
      content: Text(message),
      duration: Duration(seconds: 2),
    );
    return _getSK().currentState.showSnackBar(sb);
  }

  /// 提示高级版
  ScaffoldFeatureController<SnackBar, SnackBarClosedReason> tShowMessageWithSnackBar({
    bool removeOldMessage = true,
    SnackBar snackBar,
  }) {
    if (removeOldMessage) {
      _getSK().currentState.hideCurrentSnackBar(reason: SnackBarClosedReason.hide);
    }
    return _getSK().currentState.showSnackBar(snackBar);
  }

  /// push Widget
  Future<T> push<T extends Object>(Widget widget) {
    return Navigator.push(context, MaterialPageRoute(builder: (b) => widget));
  }

  /// 获取用MaterialPageRoute包裹的PageRoute
  MaterialPageRoute<T> routerOfPage<T>({@required BuildContext context, @required Widget widget}) {
    return MaterialPageRoute(builder: (context) => widget);
  }

  /// 更新当前sate
  void updateCurrentState() {
    setState(() {});
  }
}
